#include "userconf.h"
#include "ui_userconf.h"
#include "login.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QFile>
#include <QDebug>
#include <QStringList>
#include <QString>

UserConf::UserConf(QWidget *parent,QString a) :
    QWidget(parent),
    ui(new Ui::UserConf)
{
    ui->setupUi(this);
    setWindowTitle("修改用户信息");
    setFixedSize(314,401);
    ui->account1->setText(a);
    QSqlQuery query;
    query.prepare("select username from user where account=:acc");
    query.bindValue(":acc",a);
    query.exec();
    if(query.next())
    {
        QString username = query.value(0).toString();
        ui->username->setText(username);
    }
    //ui->username->setText(Login::username);
    connect(ui->alter,&QPushButton::clicked,this,&UserConf::on_alter_clicked);
    connect(ui->logout,&QPushButton::clicked,this,&UserConf::on_logout_clicked);
}

void UserConf::on_alter_clicked()
{
    QString acc = ui->account1->text();
    QString username = ui->username->text();
    QString oldpsw = ui->oldpsw->text();
    QString newpsw = ui->newpsw->text();
    //qDebug() << acc <<oldpsw;
    QSqlQuery query;
    query.prepare("select account,psw from user where account=:acc and psw = :psw ");
    query.bindValue(":acc",acc);
    query.bindValue(":psw",oldpsw);
    query.exec();

    if(!query.next())
    {
        QMessageBox::information(this,"警告","原密码错误！",QMessageBox::Ok);
    }
    else
    {
        query.prepare("update user set username=:username,psw=:newpsw where account=:account");
        query.bindValue(":username",username);
        query.bindValue(":newpsw",newpsw);
        query.bindValue(":account",acc);
        query.exec();
        QMessageBox::information(this,"提醒","修改成功！",QMessageBox::Ok);
        this->close();
    }
}

void UserConf::on_logout_clicked()
{
    ACCOUNT.clear();
    QMessageBox::information(this,"恭喜","成功退出！",QMessageBox::Ok);
    emit closewin();
    this->close();
}

UserConf::~UserConf()
{
    delete ui;
}
